/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
 * Copyright (c) 2013-2018 Hexagon PPM
 * 创建人：超级管理员
 * 日  期：2020-12-04 15:20
 * 描  述：项目管理
 */
var refreshGirdData;
var showContent;
var currentUserId = "";
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            currentUserId = learun.clientdata.get(['userinfo']).userId;
            page.initGird();
            page.bind();
        },
        bind: function () {
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //查询
            $('#btn_Search').on('click', function () {
                page.search($('.lr-layout-tool-left').lrGetFormData());
            });
            // 新增
            $('#lr_add').on('click', function () {
                learun.layerForm({
                    id: 'form',
                    title: '新增',
                    url: top.$.rootUrl + '/ZZDT_EC/ec_project/Form',
                    width: 700,
                    height: 610,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ProjectId');
                if (learun.checkrow(keyValue)) {
                    var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                    var updateUserID = $('#gridtable').jfGridValue('UpdateUserID');
                    if (currentUserId && (currentUserId == createUserID || currentUserId == updateUserID)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑',
                            url: top.$.rootUrl + '/ZZDT_EC/ec_project/Form?keyValue=' + keyValue + '&openType=edit',
                            width: 700,
                            height: 610,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    } else {
                        learun.alert.warning("只能编辑本人创建的项目!");
                        return;
                    }
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ProjectId');
                if (learun.checkrow(keyValue)) {
                    var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                    var updateUserID = $('#gridtable').jfGridValue('UpdateUserID');
                    if (currentUserId && (currentUserId == createUserID || currentUserId == updateUserID)) {
                        var projectIndex = $('#gridtable').jfGridValue('ProjectIndex');
                        learun.layerConfirm('是否确认删除该项！', function (res) {
                            if (res) {
                                learun.deleteForm(top.$.rootUrl + '/ZZDT_EC/ec_project/DeleteForm', { keyValue: keyValue, projectIndex: projectIndex }, function () {
                                    refreshGirdData();
                                });
                            }
                        });
                    } else {
                        learun.alert.warning("只能删除本人创建的项目!");
                        return;
                    }
                }
            });
            //项目角色
            $('#lr_setRole').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ProjectId');
                if (keyValue == "") {
                    learun.alert.warning('请选择一个项目！');
                    return false;
                }
                var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                var updateUserID = $('#gridtable').jfGridValue('UpdateUserID');
                if (currentUserId && (currentUserId == createUserID || currentUserId == updateUserID)){
                    top.learun.frameTab.open({ F_ModuleId: 'lr_frameproject000002', F_Icon: 'fa magic', F_FullName: '项目角色', F_UrlAddress: '/LR_OrganizationModule/Role/Index?ProjectId=' + keyValue });
                } else {
                    learun.alert.warning("只能选择本人创建的项目!");
                    return;
                }
            });
            //同步数据
            $('#lr_syndata').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ProjectId');
                if (keyValue == "") {
                    learun.alert.warning('请选择一个项目！');
                    return false;
                }
                var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                var updateUserID = $('#gridtable').jfGridValue('UpdateUserID');
                if (currentUserId && (currentUserId == createUserID || currentUserId == updateUserID)) {
                    var projectIndex = $('#gridtable').jfGridValue('ProjectIndex');
                    learun.layerForm({
                        id: 'form',
                        title: '同步数据',
                        url: top.$.rootUrl + '/ZZDT_EC/ec_business_table/SelectIndex?ProjectIndex=' + projectIndex + '&ProjectId=' + keyValue,
                        width: 800,
                        height: 600,
                        btn: null
                    });
                } else {
                    learun.alert.warning("只能选择本人创建的项目!");
                    return;
                }
            });
            //项目备份
            $('#lr_backup').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ProjectId');
                if (keyValue == "") {
                    learun.alert.warning('请选择一个项目！');
                    return false;
                }
                var projectIndex = $('#gridtable').jfGridValue('ProjectIndex');
                var projectName = $('#gridtable').jfGridValue('ProjectName');
                $.ajax({
                    type: 'POST',
                    url: top.$.rootUrl + "/ZZDT_EC/ec_project_bakup/ProjectBackup?rdo=" + Math.random(),
                    data: { ProjectId: keyValue, ProjectIndex: projectIndex, ProjectName: projectName },
                    traditional: true,
                    success: function (data) {
                        var data = JSON.parse(data);
                        learun.alert.success(data.info);
                    },
                    error: function (msg) {
                        learun.alert.error(msg);
                    }, complete: function (data) {
                    }
                });
            });

            //项目还原
            $('#lr_restore').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('ProjectId');
                if (keyValue == "") {
                    learun.alert.warning('请选择一个项目！');
                    return false;
                }
                var createUserID = $('#gridtable').jfGridValue('CreateUserID');
                var updateUserID = $('#gridtable').jfGridValue('UpdateUserID');
                if (currentUserId && (currentUserId == createUserID || currentUserId == updateUserID)) {
                    learun.layerConfirm('项目还原会覆盖项目所有数据，是否继续还原！', function (res) {
                        if (res) {
                            var projectIndex = $('#gridtable').jfGridValue('ProjectIndex');
                            var uploadKeyValue = Guid.NewGuid().toString();
                            learun.layerForm({
                                id: 'importfile',
                                title: '项目还原',
                                url: top.$.rootUrl + '/LR_SystemModule/Annexes/UploadForm?keyVaule=' + uploadKeyValue + "&multiple=false" + "&format=zip",
                                width: 600,
                                height: 400,
                                maxmin: true,
                                btn: ['确认'],
                                callBack: function (id) {
                                    learun.layerClose('importfile');
                                    RestoreImport(uploadKeyValue, projectIndex);
                                }
                            });
                        }
                    });
                } else {
                    learun.alert.warning("只能选择本人创建的项目!");
                    return;
                }
            });
        },
        // 初始化列表
        initGird: function () {
            $('#gridtable').jfGrid({
                url: top.$.rootUrl + '/ZZDT_EC/ec_project/GetPageList',
                headData: [
                    {
                        label: "项目名称", name: "ProjectName", width: 400, align: "left", sort: false,
                        formatter: function (value, row, op, $cell) {
                            return "<a style='color: #337ab7;text-decoration: underline;' href='javascript:showContent(\"" + row.ProjectId + "\",\"" + row.ProjectName + "\");'>" + row.ProjectName + "</a>";
                        }
                    },
                    { label: "项目代号", name: "ProjectCode", width: 150, align: "center", sort: false },
                    { label: "船名（中文）", name: "ShipNameCN", width: 150, align: "center", sort: false },
                    { label: "船名（英文）", name: "ShipNameEN", width: 150, align: "center", sort: false },
                    { label: "船型（中文）", name: "ShipTypeCNName", width: 150, align: "center", sort: false },
                    { label: "船型（英文）", name: "ShipTypeENName", width: 150, align: "center", sort: false },
                    { label: "船级社", name: "ShipSocietyName", width: 150, align: "center", sort: false },
                    { label: "船东", name: "ShipOwner", width: 150, align: "center", sort: false },
                    { label: "挂旗国（船籍国）", name: "FlagStateName", width: 150, align: "center", sort: false },
                    { label: "IMO编号", name: "IMONumber", width: 150, align: "center", sort: false },
                    { label: "创建人", name: "CreateUserName", width: 100, align: "center", sort: false },
                    {
                        label: "创建时间", name: "CreateTime", width: 150, align: "center", sort: false,
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm');
                        }
                    }
                ],
                mainId: 'ProjectId',
                sidx: "CreateTime ",
                sord: "DESC",
                isPage: true
            });
            page.search();
        },
        search: function (param) {
            param = param || {};
            $('#gridtable').jfGridSet('reload', { queryJson: JSON.stringify(param) });
        }
    };
    refreshGirdData = function () {
        $('#gridtable').jfGridSet('reload');
    };
    //查看详情
    showContent = function (id) {
        var keyValue = id;
        if (learun.checkrow(keyValue)) {
            learun.layerForm({
                id: 'form',
                title: '查看',
                url: top.$.rootUrl + '/ZZDT_EC/ec_project/Form?keyValue=' + keyValue + '&openType=look',
                width: 700,
                height: 570,
                btn: null,
                callBack: function (id) {
                    return top[id].acceptClick(refreshGirdData);
                }
            });
        }
    };
    page.init();
}

function RestoreImport(fileId, projectIndex) {
    var learun = top.learun;
    learun.loading(true, "项目还原中...");
    $.ajax({
        type: 'POST',
        url: top.$.rootUrl + "/ZZDT_EC/ec_project_bakup/ProjectRestore?rdo=" + Math.random(),
        data: { ProjectIndex: projectIndex, fileId: fileId },
        traditional: true,
        success: function (data) {
            var data = JSON.parse(data);
            learun.alert.success(data.info);
        },
        error: function (msg) {
            learun.alert.error(msg);
        }, complete: function (data) {
            learun.loading(false);
        }
    });
}